﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using log4net;
using LoveEat.Helper;
using LoveEat.MODEL;
using LoveEat.WeChat.WxPayAPI;

namespace LoveEat.WebController
{
  public  class JsApiPayPageController : BaseController
    {

        private readonly ILog _fileLog = LogManager.GetLogger(typeof(JsApiPayPageController));

      public ActionResult paytest()
      {
          
          return
       View();
    }

        /// <summary>
        /// jsapi 支付
        /// </summary>
        public ActionResult Index(string openid = "oaVFBwhD1AXiJEeZqpQx986Pl0Gc", string total_fee = "100", int wechatInfoId =1)
        {
            var context = System.Web.HttpContext.Current;
            Result resultMsg;
           WxPayConfig wxPayConfig = null;
           var wechatInfo = OperateContext.Current.BLLSession.Iwechat_infoBLL.GetListBy(w => w.id == wechatInfoId).FirstOrDefault();
            if (wechatInfo != null)
            {
                WxPayConfig.APPID = wechatInfo.appId;
                WxPayConfig.KEY = wechatInfo.payKey;
                WxPayConfig.APPSECRET = wechatInfo.secret;
                WxPayConfig.MCHID = wechatInfo.mchId;
                WxPayConfig.SSLCERT_PATH = ConfigurationManager.AppSettings["certPath"];
                WxPayConfig.SSLCERT_PASSWORD = ConfigurationManager.AppSettings["password"];
                WxPayConfig.IP = ConfigurationManager.AppSettings["LocalWWWIp"];
                WxPayConfig.NOTIFY_URL = ConfigurationManager.AppSettings["NOTIFY_URL"];
                //检测是否给当前页面传递了相关参数
                if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(total_fee))
                {
                    resultMsg = new Result() { Message = "页面传参出错,请返回重试" };
                    _fileLog.Error("This page have not get params, cannot be inited, exit...");
                    return Json(resultMsg);
                }
                //若传递了相关参数，则调统一下单接口，获得后续相关接口的入口参数
                JsApiPay jsApiPay = new JsApiPay(context);
                jsApiPay.openid = openid;
                jsApiPay.total_fee = int.Parse(total_fee);
                //JSAPI支付预处理
                try
                {
                    WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
                    ViewBag.WxJsApiParam = jsApiPay.GetJsApiParameters(); //获取H5调起JS API参数                    
                    //在页面上显示订单信息
                    _fileLog.Info("wxJsApiParam : " + ViewBag.WxJsApiParam + ",订单详情" + unifiedOrderResult.ToPrintStr());
                    //resultMsg = new Result() { Status = true, Message = "支付成功" };
                }
                catch (Exception ex)
                {

                    _fileLog.Error("下单失败，请返回重试ex:" + ex);
                    //resultMsg = new Result() { Message = "下单失败，请返回重试" };
                }

            }

            return View();
        }

        public ActionResult ResultNotifyPage()
        {
            var context = System.Web.HttpContext.Current;
            ResultNotify resultNotify = new ResultNotify(context);

            return Content(resultNotify.ProcessNotify());
        }
    }
}
